package com.umeng.soexample.mvp.view.fragment;

import android.content.ContentValues;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.lib_core.base.BaseFragment;
import com.umeng.soexample.R;
import com.umeng.soexample.bean.UserEntity;
import com.umeng.soexample.dao.USerService;
import com.umeng.soexample.di.compont.DaggerUserRegisterFragmentCompont;
import com.umeng.soexample.di.modules.UserRegisterFragmentModules;
import com.umeng.soexample.eventbus_bean.BaseEventBusEntity;
import com.umeng.soexample.eventbus_bean.ClearFragmentActivityBus;
import com.umeng.soexample.mvp.contract.UserRegisterFragmentContract;
import com.umeng.soexample.mvp.persenter.UserRegisterFragmentPresenter;

import org.greenrobot.eventbus.EventBus;

/**
 * 用户注册页面
 */
public class UserRegisterFragment extends BaseFragment<UserRegisterFragmentPresenter> implements UserRegisterFragmentContract.UserRegisterIView, View.OnClickListener {

    private ImageView iv_register_back;
    private ImageView iv_register_clear;
    private TextView tv_register_dq_phone;
    private EditText et_register_phone_zh;
    private ImageView iv_register_close_phone;
    private EditText et_register_phone_yzm;
    private TextView iv_register_yzm_get;
    private EditText et_register_y_pawd;
    private ImageView iv_register_y_see;
    private EditText et_register_kl;
    private ImageView iv_register_close_y;
    private Button btn_register_register;
    private TextView tv_register_yhfwtk;
    private TextView tv_register_yhyszc;

    private boolean pawd_see = false;//是否明文显示

    @Override
    public View createView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_user_register, null);
        return view;
    }

    @Override
    public void init(View view) {
        iv_register_back = view.findViewById(R.id.iv_register_back);
        iv_register_clear = view.findViewById(R.id.iv_register_clear);
        tv_register_dq_phone = view.findViewById(R.id.tv_register_dq_phone);
        et_register_phone_zh = view.findViewById(R.id.et_register_phone_zh);
        iv_register_close_phone = view.findViewById(R.id.iv_register_close_phone);
        et_register_phone_yzm = view.findViewById(R.id.et_register_phone_yzm);
        iv_register_yzm_get = view.findViewById(R.id.iv_register_yzm_get);
        et_register_y_pawd = view.findViewById(R.id.et_register_y_pawd);
        iv_register_y_see = view.findViewById(R.id.iv_register_y_see);
        et_register_kl = view.findViewById(R.id.et_register_kl);
        iv_register_close_y = view.findViewById(R.id.iv_register_close_y);
        btn_register_register = view.findViewById(R.id.btn_register_register);
        tv_register_yhfwtk = view.findViewById(R.id.tv_register_yhfwtk);
        tv_register_yhyszc = view.findViewById(R.id.tv_register_yhyszc);

    }

    @Override
    public void initData() {}

    @Override
    public void setupDagger() {
        DaggerUserRegisterFragmentCompont.builder().userRegisterFragmentModules(new UserRegisterFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void initListener() {
        iv_register_back.setOnClickListener(this);
        iv_register_clear.setOnClickListener(this);
        tv_register_dq_phone.setOnClickListener(this);
        iv_register_close_phone.setOnClickListener(this);
        iv_register_yzm_get.setOnClickListener(this);
        iv_register_y_see.setOnClickListener(this);
        iv_register_close_y.setOnClickListener(this);
        btn_register_register.setOnClickListener(this);
        tv_register_yhfwtk.setOnClickListener(this);
        tv_register_yhyszc.setOnClickListener(this);

        /**
         * 账后验证码密码之账号
         */
        et_register_phone_zh.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0){
                    iv_register_close_phone.setVisibility(View.VISIBLE);
                }else{
                    iv_register_close_phone.setVisibility(View.INVISIBLE);
                }
                if (s.length() > 0 && et_register_phone_yzm.getText().toString().trim().length() > 0 && et_register_y_pawd.getText().toString().trim().length() > 0){
                    btn_register_register.setBackgroundColor(Color.parseColor("#444444"));
                }else{
                    btn_register_register.setBackgroundColor(Color.parseColor("#B0B0B0"));
                }
            }
        });

        /**
         * 账后验证码密码之验证码
         */
        et_register_phone_yzm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0 && et_register_phone_zh.getText().toString().trim().length() > 0 && et_register_y_pawd.getText().toString().trim().length() > 0){
                    btn_register_register.setBackgroundColor(Color.parseColor("#444444"));
                }else{
                    btn_register_register.setBackgroundColor(Color.parseColor("#B0B0B0"));
                }
            }
        });

        /**
         * 账后验证码密码之密码
         */
        et_register_y_pawd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0 && et_register_phone_yzm.getText().toString().trim().length() > 0 && et_register_phone_zh.getText().toString().trim().length() > 0){
                    btn_register_register.setBackgroundColor(Color.parseColor("#444444"));
                }else{
                    btn_register_register.setBackgroundColor(Color.parseColor("#B0B0B0"));
                }
            }
        });

    }

    @Override
    public void success(UserEntity entity) {
        if (entity == null || entity.values == null || entity.values.size() == 0){
            Toast.makeText(getContext(), "空数据 msg -> " + entity.msg, Toast.LENGTH_SHORT).show();
            return;
        }
        Log.e("ZJC", "UserRegisterFragment -> success: -> " + entity.msg);
        Log.e("ZJC", "UserRegisterFragment -> success: -> " + entity.values.get(0).toString());

        if ("1".equals(entity.statues) && "请求成功".equals(entity.msg)){
            USerService.getInstance().delete("user", null, null);
            ContentValues values = new ContentValues();
            values.put("user_id", entity.values.get(0).user_id);
            values.put("nick_name", entity.values.get(0).nick_name);
            values.put("qr_code", entity.values.get(0).qr_code);
            values.put("user_name", entity.values.get(0).user_name);
            values.put("user_pwd", entity.values.get(0).user_pwd);
            values.put("user_sex", entity.values.get(0).user_sex);
            values.put("user_birthday", entity.values.get(0).user_birthday);
            values.put("user_height", entity.values.get(0).user_height);
            values.put("user_weight", entity.values.get(0).user_weight);
            values.put("user_head", entity.values.get(0).user_head);
            long insert = USerService.getInstance().insert("user", values);
            if (insert > 0){
                Toast.makeText(getContext(), "添加数据库成功", Toast.LENGTH_SHORT).show();
                EventBus.getDefault().post(new ClearFragmentActivityBus(BaseEventBusEntity.CLEARFRAGMENTACTIVITY));
                getActivity().finish();
            }else{
                Toast.makeText(getContext(), "添加数据库失败", Toast.LENGTH_SHORT).show();
            }
        }

    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_register_back://返回上一个页面
                getActivity().finish();
                break;
            case R.id.iv_register_clear://回到主页面
                EventBus.getDefault().post(new ClearFragmentActivityBus(BaseEventBusEntity.CLEARFRAGMENTACTIVITY));
                getActivity().finish();
                break;
            case R.id.tv_register_dq_phone://地区号
                break;
            case R.id.iv_register_close_phone:
                break;
            case R.id.iv_register_yzm_get:
                break;
            case R.id.iv_register_y_see://是否明文显示
                pawd_see = !pawd_see;
                if (pawd_see){
                    iv_register_y_see.setImageResource(R.drawable.login_password_unsee_icon);
                    //设置成明文
                    et_register_y_pawd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else{
                    iv_register_y_see.setImageResource(R.drawable.login_password_see_icon);
                    //设置成密文
                    et_register_y_pawd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.iv_register_close_y:
                et_register_phone_zh.setText("");
                break;
            case R.id.btn_register_register://注册按钮
                register();
                break;
            case R.id.tv_register_yhfwtk://有货服务条款
                break;
            case R.id.tv_register_yhyszc://有货隐私政策
                break;
        }
    }

    /**
     * 注册
     */
    private void register() {
        String zh = et_register_phone_zh.getText().toString().trim();
        if (TextUtils.isEmpty(zh)){
            Toast.makeText(getContext(), "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        String yzm = et_register_phone_yzm.getText().toString().trim();
        if (TextUtils.isEmpty(yzm)){
            Toast.makeText(getContext(), "请输入验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        String pawd = et_register_y_pawd.getText().toString().trim();
        if (TextUtils.isEmpty(pawd)){
            Toast.makeText(getContext(), "请设置登录密码", Toast.LENGTH_SHORT).show();
            return;
        }
        String kl = et_register_kl.getText().toString().trim();

        p.request(zh, pawd);
        et_register_phone_zh.setText("");
        et_register_phone_yzm.setText("");
        et_register_y_pawd.setText("");
        et_register_kl.setText("");

    }

}
/*
        iv_register_back
        iv_register_clear
        tv_register_dq_phone
        et_register_phone_zh
        iv_register_close_phone
        et_register_phone_yzm
        iv_register_yzm_get
        et_register_y_pawd
        iv_register_y_see
        et_register_kl
        iv_register_close_y
        btn_register_register
        tv_register_yhfwtk
        tv_register_yhyszc
 */
